'use strict';
import express from 'express';
import logger from 'morgan';
import bodyParser from 'body-parser';
import mysql from './lib/mysql';
import redis from 'redis';
import v1_router from './routes/v1_router';
import setting from './setting';

global.mysql = new mysql()

global.redis = redis.createClient({
    host: setting.redis.host,
    port: setting.redis.port,
    password: setting.redis.password,
    db: setting.redis.db
})
var app = express();

//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(cookieParser());
//app.use(express.static(path.join(__dirname, 'public')));

app.use('/v1', v1_router);
//app.use('/users', users);

// catch 404 and forward to error handler
app.use((req, res, next) => {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use((err, req, res, next) => {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.json(500);
});

module.exports = app;
